home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / 3DGPL 1.0 / CODE / DEMO / DEMO.C < prev   
Encoding:
C/C++ Source or Header  |  1997-03-09  |  5.3 KB  |  136 lines  |  [TEXT/MACA]

  1. /** 3DGPL *************************************************\
  2.  * A demo allowing to rotate a polygon rendering it as    *
  3.  * either ambient, shaded or textured. ( <TAB> to change  *
  4.  * rendering option ).                                    *
  5.  *                                                        *
  6.  *  If it doesn't work:                                   *
  7.  *   1) make sure you are using right hardware interface  *
  8.  *      source and your system sutisfies requirements     *
  9.  *      described in it's head comment.                   *
  10.  *   2) if this application does quit on <ENTER> but      *
  11.  *      shows nothing, adjust colour intensities assigned *
  12.  *      in main(), hardware interface function doesn't    *
  13.  *      convert them.                                     *
  14.  *   3) if the demo crashes recompile with bigger stack.  *
  15.  *                                                        *
  16.  *  (6/1995) By Sergei Savhenko. (savs@cs.mcgill.ca).     *
  17.  *  Copyright (c) 1995 Sergei Savchenko.                  *
  18.  *  THIS SOURCE CODE CAN'T BE USED FOR COMERCIAL PURPOSES *
  19.  *  WITHOUT AUTHORISATION                                 *
  20. \**********************************************************/
  21.  
  22. #include <stdio.h>
  23.  
  24. #ifdef __MWERKS__
  25. #include "hardware.h"
  26. #include "engine.h"
  27. #include "graphics.h"
  28. #include "trans.h"
  29. #else
  30. #include "../hardware/hardware.h"
  31. #include "../engine/engine.h"
  32. #include "../graphics/graphics.h"
  33. #include "../trans/trans.h"
  34. #endif
  35.  
  36. unsigned char alp=0,bet=0,gam=0;
  37. int x=0,y=0,z=200;
  38.  
  39. unsigned char texture[16][16]=
  40. {
  41.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0},
  42.  {0xef,0x55,0x55,0x55,0x55,0x55,0x55,0xe8,0xe7,0x55,0x55,0x55,0x55,0x55,0xe1,0xe0},
  43.  {0xef,0xee,0xed,0xec,0xeb,0x55,0xe9,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  44.  {0xef,0xee,0xed,0xec,0x55,0xea,0xe9,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  45.  {0xef,0xee,0xed,0x55,0x55,0x55,0x55,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  46.  {0xef,0xee,0xed,0xec,0xeb,0xea,0x55,0xe8,0xe7,0x55,0xe5,0xe4,0xe3,0xe2,0x55,0xe0},
  47.  {0xef,0x55,0x55,0x55,0x55,0x55,0xe9,0xe8,0xe7,0x55,0x55,0x55,0x55,0x55,0xe1,0xe0},
  48.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0},
  49.  {0xef,0xee,0x77,0x77,0xeb,0xea,0x76,0x76,0x76,0xe6,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  50.  {0xef,0x77,0xed,0xec,0x77,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  51.  {0xef,0x77,0xed,0xec,0xeb,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  52.  {0xef,0x77,0xed,0xec,0xeb,0xea,0x76,0xe8,0xe7,0x76,0xe5,0x75,0xe3,0xe2,0xe1,0xe0},
  53.  {0xef,0x77,0xed,0x77,0x77,0xea,0x76,0x76,0x76,0xe6,0xe5,0x75,0xe3,0xe2,0x75,0xe0},
  54.  {0xef,0x77,0xed,0xec,0x77,0xea,0x76,0xe8,0xe7,0xe6,0xe5,0x75,0xe3,0xe2,0x75,0xe0},
  55.  {0xef,0xee,0x77,0x77,0x77,0xea,0x76,0xe8,0xe7,0xe6,0xe5,0x75,0x75,0x75,0x75,0xe0},
  56.  {0xef,0xee,0xed,0xec,0xeb,0xea,0xe9,0xe8,0xe7,0xe6,0xe5,0xe4,0xe3,0xe2,0xe1,0xe0}
  57. };
  58.  
  59. struct M_polygon a= {M_AMBIENT,56,NULL,0,0,0,0,4,{0,3,6,9,0}};
  60. struct M_polygon s= {M_SHADED,0,NULL,0,0,0,0,4,{0,5,3,42,6,58,9,21,0,11}};
  61. struct M_polygon t= {M_TEXTURED,0,(unsigned char*)texture,4,6,0,3,4,
  62.                     {0,0,64,3,64,64,6,64,0,9,0,0,0,0,64}};
  63. struct M_polygon *p=&t;
  64. int m_no_verteces=4;
  65. int verteces[]={-50,50,0,50,50,0,50,-50,0,-50,-50,0};
  66. int m_no_vectors=2;
  67. int vectors[]={100,0,0,0,100,0};
  68.  
  69. /**********************************************************\
  70.  * being called to treat key press.                       *
  71. \**********************************************************/
  72.  
  73. void app_handler(int kk)
  74. {
  75.  switch(kk)
  76.  {
  77.   case HW_KEY_ENTER:       HW_quit_event_loop(); break;
  78.   case HW_KEY_ARROW_RIGHT: gam+=10; break;
  79.   case HW_KEY_ARROW_LEFT:  gam-=10; break;
  80.   case HW_KEY_ARROW_UP:    bet+=10; break;
  81.   case HW_KEY_ARROW_DOWN:  bet-=10; break;
  82.   case 'X': case 'x':      alp+=10; break;
  83.   case 'S': case 's':      alp-=10; break;
  84.   case 'A': case 'a':      z-=10;   break;
  85.   case 'Z': case 'z':      z+=10;   break;
  86.   case HW_KEY_TAB: if(p==&a) p=&s; else if(p==&s) p=&t; else p=&a;
  87.  }
  88. }
  89.  
  90. /**********************************************************\
  91.  * being called to render a frame.                        *
  92. \**********************************************************/
  93.  
  94. void app_main(void)
  95. {
  96.  int tmp_verteces[50];
  97.  int tmp_vectors[50];
  98.  char str[100];
  99.  
  100.  G_clear();
  101.  
  102.  T_set_self_rotation(alp,bet,gam);
  103.  T_self_rotation(verteces,tmp_verteces,m_no_verteces);
  104.  T_translation(tmp_verteces,tmp_verteces,m_no_verteces,x,y,z);
  105.  T_self_rotation(vectors,tmp_vectors,m_no_vectors);
  106.  M_render_polygon(p,tmp_verteces,tmp_vectors);
  107.  
  108.  sprintf(str,"3DGPL <%d %d %d %d>",alp,bet,gam,z); G_text(0,0,str,63);
  109.  if(p==&a) G_text(200,0,"AMBIENT",63);
  110.  if(p==&s) G_text(200,0,"SHADED",63);
  111.  if(p==&t) G_text(200,0,"TEXTURED",63);
  112.  G_text(0,190,"<ARROWS> <TAB> <A> <Z> <S> <X> <ENTER>",63);
  113.  
  114.  HW_blit();
  115. }
  116.  
  117. /**********************************************************\
  118.  * assignes colours and starts the event loop.            *
  119. \**********************************************************/
  120.  
  121. int main(void)
  122. {
  123.  struct HW_colour pal[256];
  124.  int i;
  125.  
  126.  for(i=0;i<256;i++) { pal[i].hw_r=i; pal[i].hw_g=i; pal[i].hw_b=i; }
  127.  //for(i=0;i<256;i++) { pal[i].hw_r=i; pal[i].hw_g=0; pal[i].hw_b=0; }
  128.  HW_open_screen(NULL,"3DGPL",pal,G_init_graphics()); T_init_math();
  129.  HW_run_event_loop(app_main,app_handler);
  130.  HW_close_screen();
  131.  
  132.  return(1);
  133. }
  134.  
  135. /**********************************************************/
  136.